//
//  JCPlanCell.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/4/1.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCPlanCell.h"

@interface JCPlanCell ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel * numLabel;// 序号

@property (nonatomic, strong) UILabel * title;// 标题



@end


@implementation JCPlanCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self initUI];
    }
    return self;
    
}

+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView{
    
    static NSString * cellIndentfile = @"JCPlanCell";
    JCPlanCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentfile];
    
    if (!cell) {
        cell = [[JCPlanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentfile];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (void)initUI{
    
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [self.deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.deleteBtn];
    
    self.numLabel = [UILabel new];
    self.numLabel.text = @"1";
    self.numLabel.layer.cornerRadius = 7;
    self.numLabel.layer.masksToBounds = YES;
    self.numLabel.layer.borderWidth = 1;
    self.numLabel.layer.borderColor = [UIColor colorWithHexString:@"333333"].CGColor;
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.font = [UIFont systemFontOfSize:10];
    [self addSubview:self.numLabel];
    
    self.title = [UILabel new];
    self.title.text = @"代扣金额";
    self.title.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.title];
    
    self.amount = [UITextField new];
    self.amount.placeholder = @"请输入正确的金额";
    self.amount.keyboardType = UIKeyboardTypeNumberPad;
    [self.amount setValue:[UIColor colorWithHexString:@"#c0c0c7"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.amount  setValue:KSystemFont(__I6Width(14)) forKeyPath:@"_placeholderLabel.font"];
    self.amount.font = KSystemFont(__I6Width(14));
    self.amount.textColor = [UIColor blackColor];
    self.amount.delegate = self;
    self.amount.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.amount];
    
    self.buckleLabel = [UILabel new];
    self.buckleLabel.text = @"还款金额";
    self.buckleLabel.font = KSystemFont(12);
    self.buckleLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.buckleLabel];
    
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(__I6Width(15));
        make.top.equalTo(self).offset(__I6Height(20));
        make.width.height.mas_offset(25);
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.deleteBtn.mas_right).offset(__I6Width(15));
        make.centerY.equalTo(self.deleteBtn);
        make.width.height.mas_offset(14);
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numLabel.mas_right);
        make.centerY.equalTo(self.numLabel);
        make.height.mas_offset(__I6Height(30));
    }];
    
    [self.amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-__I6Width(15));
        make.centerY.equalTo(self.title);
        make.left.equalTo(self.title.mas_right).offset(10);
        make.height.mas_offset(25);
    }];
    
    [self.buckleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.amount.mas_right);
        make.left.equalTo(self);
        make.top.equalTo(self.amount.mas_bottom).offset(__I6Height(10));
    }];
    
}


- (void)deleteAction:(UIButton *)btn{

    if ([_delegate respondsToSelector:@selector(clickDeleteAction:)]) {
        [_delegate clickDeleteAction:btn];
    }
}

//文本彻底结束编辑时调用
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text.length != 0) {
        CGFloat money = ([textField.text doubleValue] - [self.manageModel.fee doubleValue] *0.01)/(1+[self.manageModel.rate doubleValue]);

        NSLog(@"money :%f",money);
        self.buckleLabel.text = [NSString stringWithFormat:@"还款金额%0.2f",money];

        if ([_delegate respondsToSelector:@selector(getAmountText:andIndex:)]) {
            [_delegate getAmountText:textField.text andIndex:self.index];
        }
    }
   
}

- (void)getIndexNumber:(NSInteger)index{
    
    self.numLabel.text = [NSString stringWithFormat:@"%d",index+1];
}

- (void)getAmountText:(NSString *)str{
    self.amount.text = str;
    CGFloat money = ([str doubleValue] - [self.manageModel.fee doubleValue] *0.01)/(1+[self.manageModel.rate doubleValue]);
    NSLog(@"money :%f",money);
    if ([str isEqualToString:@""]) {
        self.buckleLabel.text = @"还款金额";

    }else{
        self.buckleLabel.text = [NSString stringWithFormat:@"还款金额%0.2f",money];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
